﻿import configparser
import os
import filecmp

# 递归复制目录下所有文件
def copyFiles(sourceDir, targetDir, ex_fods, ex_files, ex_fsubname):
    ''' 将文件拷到目标目录，忽略指定规则的目录或文件 '''
    # 排除不要的目录
    if ex_fods.count(sourceDir.split('\\')[-1]) > 0:
        return
    # 创建不存在的目录
    if not os.path.exists(targetDir):
        os.makedirs(targetDir)
    for file in os.listdir(sourceDir):
        sourceFile = os.path.join(sourceDir, file)
        targetFile = os.path.join(targetDir, file)
        if os.path.isfile(sourceFile):
            # 排除不要的文件
            if ex_files.count(file):
                continue
            # 排除文件名包含指定字符串的文件
            isEX = False
            for ex_str in ex_fsubname:
                if file.find(ex_str) >= 0:
                    isEX = True
                    break;
            if isEX:
                continue
            if not os.path.exists(targetFile) or (filecmp.cmp(targetFile, sourceFile) == False):
                open(targetFile, "wb").write(open(sourceFile, "rb").read())
        if os.path.isdir(sourceFile):
            copyFiles(sourceFile, targetFile, ex_fods, ex_files, ex_fsubname)

def findTargetDir(root_path, raw_path, ex_fods, ex_files, ex_fsubname, cpy_conditions, search_subdir, search_deep, max_deep):
    ''' 获取符合条件的目标目录并将文件拷贝过去 '''
    if search_deep > max_deep:
        return
    # 取得当前目录下的所有子目录
    files = list()
    for fodorfile in os.listdir(root_path):
        Path = os.path.join(root_path, fodorfile)
        if os.path.isdir(Path) and search_subdir == True:
            findTargetDir(Path, raw_path, ex_fods, ex_files, ex_fsubname, cpy_conditions, search_subdir, search_deep + 1, max_deep)
            # 取得子目录下的所有文件
        elif os.path.isfile(Path):
            files.append(fodorfile)
    # 如果这些文件 包括条件中的全部文件 则认定为目标路径
    isTargetDir = True;
    for condition in cpy_conditions:
        if files.count(condition) <= 0:
            isTargetDir = False
            break
    if isTargetDir:
        print(root_path)
        copyFiles(raw_path, root_path, ex_fods, ex_files, ex_fsubname)

def readSection(lines, start, endflag):
    ''' 读取一个版块的所有选项 '''
    temp = list()
    for j in range(start, len(lines)):
        if lines[j][0] == '#' or lines[j][0] == ' ' or lines[j][0] == '\n':
                    continue
        if lines[j][0] == endflag:
            i = j
            break
        if lines[j][-1] == '\n':
            lines[j] = lines[j][0:-1]
        temp.append(lines[j])
    return temp
def loadConfigByReadFile():
    ''' 读取配置文件 '''
    # 读取配置文件
    FILE_NAME = "update.ini"

    # 读出整个配置文件  所有行
    cfobj = open(FILE_NAME, "r")
    lines = cfobj.readlines()
    cfobj.close()

    source_path = list()
    dest_path = list()
    # 手动读取多个同级选项 保存在list中
    ex_fods = list()
    ex_files = list()
    ex_fsubname = list()
    cpy_conditions = list()
    for i in range(0, len(lines)):
        # 忽略注释 和 空行
        if lines[i][0] == '#' or lines[i][0] == ' ' or lines[i][0] == '\n':
            continue
        # 去掉行尾换行符
        if lines[i][-1] == '\n':
            lines[i] = lines[i][0:-1]
        # 碰到版块标记 调用函数读取整个版块内容
        if lines[i] == "[SEARCH_SUBDIR]":
            if str.upper(readSection(lines, i + 1, '[')[0]) == "TRUE":
                search_subdir = True
            elif str.upper(readSection(lines, i + 1, '[')[0]) == "FALSE":
                search_subdir = False
        elif lines[i] == "[SEARCH_DEEP]":
            search_deep = int(readSection(lines, i + 1, '[')[0])
        elif lines[i] == "[SOURCE_PTAH]":
            source_path = readSection(lines, i + 1, '[')
            i += len(source_path)
        elif lines[i] == "[DEST_PATH]":
            dest_path = readSection(lines, i + 1, '[')
            i += len(dest_path)
        elif lines[i] == "[EXCLUDE FOLDERS]":
            #print ("[EXCLUDE FOLDERS]")
            ex_fods = readSection(lines, i + 1, '[')
            i += len(ex_fods)
            #for line in ex_fods:
            #    print(line)
        elif lines[i] == "[EXCLUDE FILES]":
            ex_files = readSection(lines, i + 1, '[')
            i += len(ex_files)
        elif lines[i] == "[EXCLUDE FILESNAME SUBSTR]":
            ex_fsubname = readSection(lines, i + 1, '[')
            i += len(ex_fsubname)
        elif lines[i] == "[COPY CONDITION]":
            cpy_conditions = readSection(lines, i + 1, '[')
            i += len(cpy_conditions)
    for targetDir in dest_path:
        findTargetDir(targetDir, source_path[0], ex_fods, ex_files, ex_fsubname, cpy_conditions, search_subdir, 0, search_deep)

search_subdir = False
search_deep = 1
loadConfigByReadFile()



